<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * ****** 2014118 by liub  
 * 投资积分
 * 
 * */
class Invest_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    /*
     * 
    
    //初始化用戶投資积分
    function init_credit($parms = array()){
		if(isset($parms['user_id']) &&  $parms['user_id'] > 0){
			$this->db->insert('rsh_invest', $parms);
    		return $this->db->affected_rows();
		}else{
			return false;
		}
    }
    
    //获取用户投资积分
    function get_row($user_id = 0){
    	$user_id = intval($user_id);
		if($user_id < 1){
			return false;
		}
		$key = 'rsh_invest_' . $user_id;
		$this->mycache->delete($key);//@todo
		$row = $this->mycache->get_array($key);
		if($row == false){
			$query = $this->db->get_where('rsh_invest_rank', array('user_id' => $user_id));
			$row = $query->row_array();
			if($row){
				$this->mycache->set_array($key,$row);
			}
		}
		return $row;
    }
    
     //添加日志
    function add_log($parms = array()){
    	if(isset($parms['user_id']) &&  $parms['user_id'] > 0){
			$this->db->insert('rsh_invest_log', $parms);
    		return $this->db->affected_rows();
		}else{
			return false;
		}
    }
    
     //增加投资积分
     @param $log array(user_id,type_id,op,credit_value,remark,op_user,add_time,addip)
    function incr_credit($log = array()){
    	//$user_id = isset($data['user_id']) ? intval($data['user_id']) : 0;
    	$res = false;
    	if(isset($log['user_id']) && $log['user_id'] > 0){
    		$res = self::add_log($log);
    		if($res){
    			$credit = self::get_row($log['user_id']);
    			if(!$credit){
    				//初始化用户投资积分
			    	$init = array(
			    		'user_id'       => $log['user_id'],
			    		'credit_value'  => 0,
			    		'no_available'  => 0,
			    		'available'     => 0,
			    		'op_user'       => $log['op_user'],//@todo 操作的管理员
			    		'add_time'      => $log['add_time'],
			    		'addip'         => $log['addip'],
			    		'updatetime'    => $log['add_time'],
			    		'updateip'      => $log['addip'],
			    	);
					if(self::init_credit($init)){
						$credit = $init;
					}
    			}
    			$tmp = array(
    				'user_id' => $log['user_id'],
    				'credit_value' => $log['op'] == 1 ? ($credit['credit_value'] + $log['credit_value']) : ($credit['credit_value'] - $log['credit_value']),
    				'available' => $log['op'] == 1 ? ($credit['available'] + $log['credit_value']) : ($credit['available'] - $log['credit_value']),
    				'op_user' => $log['op_user'],
    				'updatetime' => $log['add_time'],
    				'updateip' => $log['addip'],
    			);
    			$res = self::update_credit($tmp);
    		}
    	}
    	return $res;
    }
    
    
     //更新用户投资积分
    function update_credit($parms = array()){
    	if(isset($parms['user_id'])){
			$user_id = $parms['user_id'];
			unset($parms['user_id']);
    		$this->db->where('user_id', $user_id);
			$this->db->update('rsh_credit', $parms);
			$res = $this->db->affected_rows();
	    	if($res > 0){
				$key = 'rsh_credit_'.$user_id;
				$this->mycache->delete($key);
			}
			return $res;
		}else{
			return false;
		}
    }
    
    */
    
    
 	/**
     * 获取投资VIP等级列表
     * 
     * */
    function get_invest_rank_list(){
    	$_order = " order by vip_name ";
    	$_sql = "select * from rsh_invest_rank where fstatus = 0  {$_order} "  ;
    	$query = $this->db->query($_sql);
    	return $query->result_array();
    }
    
    
 	/**
     * 获取投资VIP等级
     * 
     * */
    function get_invest_rank_by_id($id){
    	$_sql = "select pic from rsh_invest_rank where id = '{$id}' and fstatus = 0   "  ;
    	$query = $this->db->query($_sql);
    	$row = $query->row(); 
    	if($row>0){
	    	return $row->pic;
    	}else{
    		return false;
    	}
    }
    
    /**
     * 添加投资VIP等级
     * 
     * */
    function add_invest_rank($parms){
    	$this->db->insert('rsh_invest_rank', $parms);
		$this->mycache->delete('rsh_invest_rank');//删除缓存
    	return $this->db->affected_rows();
    }
    
	/**
     * 删除投资VIP等级列表
     * 
     * */
	function del_invest_rank($id){
		$r = $this->db->query('update rsh_invest_rank set fstatus  = 1 WHERE id='.$id.' LIMIT 1');
		$this->mycache->delete('rsh_invest_rank');//删除缓存
		return $this->db->affected_rows();
	}
	
	/**
	 * 
	 * 更新投资VIP等级
	 * */
	function update_invest_rank($id,$parms = array()){
	    $this->db->where('id', $id);
		$this->db->update('rsh_invest_rank', $parms);
		$this->mycache->delete('rsh_invest_rank');
		return $this->db->affected_rows();
	}
    
    
 	function get_rank_id($score){
    	$key = 'rsh_invest_rank';
    	$this->db->select('id,maxscore,minscore');
    	$query = $this->db->get('rsh_invest_rank');
    	$list = $query->result_array();
    	
    	foreach($list as $v){
    		if($score >= $v['minscore'] && $score <= $v['maxscore']){
    			return $v['id'];
    		}
    	}
    	return false;
    }
    
    
   function get_pic($score){
    	$key = 'rsh_invest_rank';
    	//$this->mycache->delete($key);//@todo
    	$list = $this->mycache->get_array($key);
    	if($list == false){
    		$this->db->select('id,maxscore,minscore,pic');
    		$query = $this->db->get('rsh_invest_rank');
    		$list = $query->result_array();
    		$this->mycache->set_array($key, $list, 0);
    	}
    	
    	foreach($list as $v){
    		if($score >= $v['minscore'] && $score <= $v['maxscore']){
    			return $v['pic'];
    		}
    	}
    	return false;
    }
    
    
}